package com.yu.yupicture.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yu.yupicture.Untils.ThrowUntil;
import com.yu.yupicture.common.BaseReponse;
import com.yu.yupicture.common.DeleteRequest;
import com.yu.yupicture.common.Result;
import com.yu.yupicture.exception.ErrorCode;
import com.yu.yupicture.model.domain.dto.space.SpaceQueryRequest;
import com.yu.yupicture.model.domain.dto.spaceUser.SpaceUserAddRequest;
import com.yu.yupicture.model.domain.dto.spaceUser.SpaceUserEditRequest;
import com.yu.yupicture.model.domain.entry.SpaceUser;
import com.yu.yupicture.model.domain.entry.User;
import com.yu.yupicture.model.domain.vo.SpaceUserVO;
import com.yu.yupicture.service.SpaceUserService;
import com.yu.yupicture.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/spaceUser")
@Api(tags = "团队空间接口")
@RequiredArgsConstructor
public class SpaceUserController {

    private final SpaceUserService spaceUserService;
    private final UserService userService;

    @PostMapping("/add")
    @ApiOperation("添加团队空间成员")
    public BaseReponse<Void> addSpaceUser(@RequestBody SpaceUserAddRequest spaceUserAddRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        spaceUserService.addSpaceUser(spaceUserAddRequest, user);
        return Result.success(null);
    }

    @PostMapping("/edit")
    @ApiOperation("更新团队空间成员")
    public BaseReponse<Void> updateSpaceUser(@RequestBody SpaceUserEditRequest spaceUserEditRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        spaceUserService.updateSpaceUser(spaceUserEditRequest, user);
        return Result.success(null);
    }

    @PostMapping("/delete")
    @ApiOperation("删除团队空间成员")
    public BaseReponse<Void> deleteSpaceUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        spaceUserService.deleteSpaceUserById(deleteRequest.getId(), user);
        return Result.success(null);
    }

    @PostMapping("list")
    @ApiOperation("获取团队空间成员列表")
    public BaseReponse<List<SpaceUserVO>> getSpaceUserList(@RequestBody SpaceQueryRequest spaceQueryRequest) {
        List<SpaceUserVO> spaceUserList = spaceUserService.getSpaceUserList(spaceQueryRequest);
        return Result.success(spaceUserList);
    }

    /**
     * 获取团队空间成员
     */
    @PostMapping("/get")
    @ApiOperation("获取单个团队空间成员信息")
    public BaseReponse<SpaceUserVO> getSpaceUser(@RequestBody SpaceQueryRequest spaceQueryRequest) {
        SpaceUserVO spaceUserVO = spaceUserService.getSpaceUser(spaceQueryRequest);
        return Result.success(spaceUserVO);
    }

    /**
     * 查询我加入的团队空间
     */
    @PostMapping("/list/my")
    @ApiOperation("查询我加入的团队空间")
    public BaseReponse<List<SpaceUserVO>> getMySpaceList(HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        ThrowUntil.throwIf(user == null, ErrorCode.NOT_LOGIN_ERROR, "用户未登录");
        List<SpaceUser> spaceUserList = spaceUserService.list(new QueryWrapper<SpaceUser>().eq("userId", user.getId()));
        List<SpaceUserVO> result = spaceUserService.spaceUserListToVo(spaceUserList);
        return Result.success(result);
    }
}
